package com.clz.hyperit.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Note;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.service.NoteService;
import com.clz.hyperit.service.UserDetailService;
import com.clz.hyperit.util.ResultMessage;
import com.clz.hyperit.vo.Note_user;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.UserArticle;
@RestController
@RequestMapping(value = "/note")
public class NoteController {
	private ResultMessage result = null;
	private final Logger logger = LoggerFactory
			.getLogger(ArticleController.class);
	
	@Autowired
	private UserDetailService userDetailService;
	
	@Autowired
	private NoteService noteService;
	/**
	 * 保存随笔/草稿
	 * @param note
	 * @return
	 */
	@RequestMapping(value = "/saveNote", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> saveNote(Note note){
		result = new ResultMessage();
		result.setServiceResult(true);
		Integer note_id  = noteService.saveNote(note);
		Note note_1  = noteService.getbyid(note_id);
		UserDetail ud = userDetailService.getUserInfo(note_1.getUserDetail_id());
		Note_user note_user = new Note_user(ud.getName(),ud.getHead(),note_1);
		result.getResultParm().put("note_user",note_user);
		if(note.getStatus()==0){
			result.setResultInfo("发表成功");
		}
		else{
			result.setResultInfo("保存草稿成功");
		}
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 删除随笔
	 * @param note_id
	 * @param userdetail_id
	 * @return
	 */
	@RequestMapping(value = "/deleteNote", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteNote(Integer note_id,Integer userdetail_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		Note note  = noteService.getbyid(note_id);
		if(note.getUserDetail_id()!=userdetail_id){
			result.setServiceResult(false);
			result.setResultInfo("没有权限操作");
		}
		else{
			noteService.deleteNote(note_id,userdetail_id);
			result.setResultInfo("删除成功");
		}
		
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}
	/**
	 * 更新随笔
	 * @param note
	 * @param userdetail_id
	 * @return
	 */
	@RequestMapping(value = "/updateNote", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updateNote(Note note,Integer userdetail_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		//更新note表
		Note note_1 = noteService.updateNote(note,userdetail_id);
		UserDetail ud = userDetailService.getUserInfo(note_1.getUserDetail_id());
		Note_user note_user = new Note_user(ud.getName(),ud.getHead(),note_1);
		result.setResultInfo("更新随笔成功");
		result.getResultParm().put("note_user",note_user);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 查找所有 -按时间排序
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping(value = "/findNote", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findNote(PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Note> note_list = noteService.findNote(pageInfo);
		if (note_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		}
		List<Note_user> result_list = getNoteUser(note_list); 
		result.getResultParm().put("note_list", result_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 查找一篇随笔
	 * @param userdetail_id
	 * @param note_id
	 * @return
	 */
	@RequestMapping(value = "/findOneNote", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findOneNote(Integer userdetail_id,Integer note_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		Note note  = noteService.getbyid(note_id);
		boolean collect_flag,follow_flag;
		if(note==null){
			result.setServiceResult(false);
			result.setResultInfo("文章不存在");
		}
		else{
//			article.setContent_md(null);
			//获取作者id
			UserDetail ud = userDetailService.getUserInfo(note.getUserDetail_id());
			if(userdetail_id==null||ud.getId()!=userdetail_id){
				note.setReading(note.getReading()+1);
				noteService.updateNote(note);
				note  = noteService.getbyid(note_id);
			}
			//获取收藏和关注状态
			if(userdetail_id!=null){
				collect_flag = noteService.getCollection(note_id,userdetail_id);
				follow_flag = noteService.getFollow(note.getUserDetail_id(),userdetail_id);
				result.getResultParm().put("collect",collect_flag);
				result.getResultParm().put("follow",follow_flag);
			}		
			result.getResultParm().put("user", ud);
			result.getResultParm().put("note",note);	
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 查找个人所有随笔
	 * @param userdetail_id
	 * @return
	 */
	@RequestMapping(value = "/findPersonNote", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findPersonNote(Integer userdetail_id,Integer status,PageInfo pageInfo){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Note> note_list = noteService.findPersonNote(userdetail_id,status,pageInfo);
		UserDetail ud = userDetailService.getUserInfo(userdetail_id);
		result.getResultParm().put("user", ud);
		result.getResultParm().put("note_list",note_list);
		result.setResultInfo("查找个人随笔");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 查找所有 -按阅读排序
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping(value = "/findNoteByRead", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findNoteByRead(PageInfo pageInfo,Integer status){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Note> note_list = noteService.findNoteByRead(pageInfo,status);
		if (note_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		}
		List<Note_user> result_list = getNoteUser(note_list); 
		result.getResultParm().put("note_list", result_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 查找所有 -按收藏排序
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping(value = "/findNoteByCollect", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findNoteByCollect(PageInfo pageInfo,Integer status){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Note> note_list = noteService.findNoteByCollect(pageInfo,status);
		if (note_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		}
		List<Note_user> result_list = getNoteUser(note_list); 
		result.getResultParm().put("note_list", result_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 搜索
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping(value = "/searchNote", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> searchNote(PageInfo pageInfo,String search,Integer status){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Note> note_list = noteService.searchNote(pageInfo,search,status);
		if (note_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		}
		List<Note_user> result_list = getNoteUser(note_list); 
		result.getResultParm().put("note_list", result_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	private List<Note_user> getNoteUser(List<Note> note_list) {
		List<Note_user> list = new ArrayList<Note_user>();
		Note_user note_user = null;
		UserDetail ud = null;
		for(Note note : note_list){
			ud = userDetailService.getUserInfo(note.getUserDetail_id());
			note.setContent_md(null);
			note.setContent_html(null);
			note_user = new Note_user(ud.getName(),ud.getHead(),note);
			list.add(note_user);
		}
		return list;
	}
	
}
